package st.business.tepth.business.history;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.HistoryConditionBean;
import st.business.tepth.business.bean.HistoryResultBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-02 15:39
 */

public class HistoryModel implements HistoryContract.Model {

    @Override
    public Disposable getCondition(String token, final OnDataConvertListener<HistoryConditionBean> listener) {
        return RxRestClient.builder()
                .url(Url.GetHistoryCondition)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToConditionList(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable getHistoryData(String token, String busNumber,
                                     String startDate, String endDate,
                                     String classification, String workType,
                                     int pageSize, int pageNumber,
                                     final OnDataConvertListener<List<HistoryResultBean>> listener) {
        return RxRestClient.builder()
                .url(Url.GetHistoryData)
                .params("token", token)
                .params("busNumber", busNumber)
                .params("startDate", startDate)
                .params("endDate", endDate)
                .params("classification", classification)
                .params("workType", workType)
                .params("pageSize", pageSize)
                .params("pageNumber", pageNumber)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToHistoryList(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    private HistoryConditionBean convertToConditionList(String response) {
        return new ObjConverter<HistoryConditionBean>().convert(response
                , new TypeReference<HistoryConditionBean>() {
                });
    }

    private List<HistoryResultBean> convertToHistoryList(String response) {
        return new ObjConverter<List<HistoryResultBean>>().convert(response
                , new TypeReference<List<HistoryResultBean>>() {
                });
    }
}
